<?php
/**
 * @author fld 2021-06-16
 * @desc 管理员控制类
 */
namespace App\Http\Controllers\Admin;

use App\Service\Admin\AdminService;
use App\Service\Admin\RoleService;
use Illuminate\Http\Request;

class AdminController extends AdminBaseController
{
    /**
     * @author fld 2021-06-17
     * @desc 管理员列表
     * @param Request $request
     * @param AdminService $adminService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(AdminService $adminService,RoleService $roleService){
        $list = $adminService->getAdminList();
        $list['role_list'] = $roleService->getAllRoleArray();
        return view('admin/admin/index', $list);
    }

    /**
     * @author fld 2021-06-18
     * @desc 新增管理员
     * @param Request $request
     * @param AdminService $adminService
     * @param RoleService $roleService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function add(Request $request,AdminService $adminService,RoleService $roleService){
        if($request->isMethod('post')){
            $result = $adminService->addOrUpdateAdmin();
            if($result['code'] == 0){
                return $this->success(['url'=>url('admin/admin/index'),'message'=>$result['message']]);
            }else{
                return $this->failed($result['code'], ['message'=>$result['message']]);
            }
        }else{
            $data = [];
            $data['role_list'] = $roleService->getRoleList();
            $data['passwd'] = getRandomStr(8, false);
            return view('admin/admin/admin_add', $data);
        }
    }

    /**
     * @author fld 2021-06-18
     * @desc 修改管理员
     * @param Request $request
     * @param AdminService $adminService
     * @param RoleService $roleService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request,AdminService $adminService,RoleService $roleService){
        if($request->isMethod('post')){
            $result = $adminService->addOrUpdateAdmin();
            if($result['code'] == 0){
                return $this->success(['url'=>url('admin/admin/index'),'message'=>$result['message']]);
            }else{
                return $this->failed($result['code'], ['message'=>$result['message']]);
            }
        }else{
            $data = [];
            $data['item'] = $adminService->getAdminByCondition(['id'=>$request->get('id')]);
            $data['role_list'] = $roleService->getRoleList();
            return view('admin/admin/admin_edit', $data);
        }
    }


    /**
     * @author fld 2021-06-18
     * @desc 删除管理员信息
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(AdminService $adminService){
        $id = request()->post('id');
        $result = $adminService->deleteAdminById($id);
        if($result['code'] == 0){
            return $this->success(['url'=>url('admin/admin/index'),'message'=>$result['message']]);
        }else{
            return $this->failed($result['code'], ['message'=>$result['message']]);
        }
    }

    /**
     * @author fld 2021-06-21
     * @desc  重置管理员密码
     * @param Request $request
     * @param AdminService $adminService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function reset_pwd(Request $request,AdminService $adminService){
        if($request->isMethod('post')){
            $result = $adminService->resetAdminPwd();
            if($result['code'] == 0){
                return $this->success(['url'=>url('admin/admin/index'),'message'=>$result['message']]);
            }else{
                return $this->failed($result['code'], ['message'=>$result['message']]);
            }
        }else{
            $id = request()->post('id');
            $item = $adminService->getAdminByCondition(['id'=>$id]);
            $data['name'] = $item['admin_name'];
            $data['true_name'] = $item['true_name'];
            $data['id'] = $id;
            $data['passwd'] = getRandomStr(8, false);
            return view('admin/admin/admin_resetpwd', $data);
        }
    }

    /**
     * @author fld 2021-07-18
     * @desc 更新密码
     * @param Request $request
     * @param AdminService $adminService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function update_pwd(Request $request,AdminService $adminService){
        if($request->isMethod('post')){
            $result = $adminService->updatePwd();
            if($result['code'] == 0){
                return $this->success(['url'=>url('admin/admin/index'),'message'=>$result['message']]);
            }else{
                return $this->failed($result['code'], ['message'=>$result['message']]);
            }
        }else{
            $admin = session('admin');
            return view('admin/admin/admin_updatepwd');
        }
    }
}
